<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Товары</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>
    <script type="text/javascript" src="resources/js/goodwill.js"></script>
    <style type="text/css">

        <%--Стили для колонок таблицы товаров--%>

        <%--Наименование--%>
        .c0
        {
            font-weight:bold;
            background-color:whitesmoke;
            white-space:nowrap;
            width:1%;
        }

        <%--Код--%>
        .c1
        {
            font-weight:bold;
            background-color:whitesmoke;
            white-space:nowrap;
            width:1%;
        }

        <%--о.е.--%>
        .c2
        {
            white-space:nowrap;
            width:1%;
        }

        <%--Применимость--%>
        .c3
        {
            white-space:nowrap;

        }

        <%--В наличии--%>
        .c4
        {
            background-color:whitesmoke;
            white-space:nowrap;
            width:80px;
        }

        <%--Цена--%>
        .c5
        {
            background-color:whitesmoke;
            white-space:nowrap;
            width:80px;
        }

        <%--Действия--%>
        .c6
        {
            white-space:nowrap;
            width:80px;
        }
    </style>

    <script type="text/javascript">

        function addGoodInOrder(goodId, count)
        {
            _addGoodInOrder(goodId, count);
        }

    </script>
</head>
<body>
<f:view>

    <jsp:include page="menu.jsp"/>
    <jsp:include page="orderClientInfo.jsp"/>
    <jsp:include page="orderTopBar.jsp"/>
    <jsp:include page="status.jsp"/>
    <jsp:include page="messages.jsp"/>

    <a4j:form id="jsForm" prependId="false">
        <a4j:jsFunction name="_addGoodInOrder"
                        action="#{ClientGoodsSearchBean.addGoodInOrder}"
                        oncomplete="#{rich:component('modalNotifications')}.show()">
            <a4j:actionparam name="good_id" assignTo="#{ClientGoodsSearchBean.good.id}"/>
            <a4j:actionparam name="count" assignTo="#{ClientGoodsSearchBean.count}"/>
        </a4j:jsFunction>
    </a4j:form>

    <h:form prependId="false">

        <h:panelGroup styleClass="" layout="block">
            <%-- -------------- Поиск -------------- --%>
            <h:panelGroup styleClass="" layout="block" style="background-color:whitesmoke;padding-left:2px">
                <h:outputLabel value="Поиск по оригинальному номеру запчасти" styleClass="b"/>
                <h:inputText value="#{SessionBean.goodsFilter.oe}" style="margin-left:10px"/>
                <a4j:commandButton value="Поиск" action="#" styleClass="btnsz"
                                   style="margin-left:10px"
                                   oncomplete="document.location='ManagerGoodsSearchPage.jsf'">
                    <a4j:actionparam name="goodTypeId" assignTo="#{SessionBean.goodsFilter.goodTypeId}" value="-1"/>
                </a4j:commandButton>
            </h:panelGroup>

            <%-- -------------- Путь -------------- --%>
            <h:panelGroup styleClass="" layout="block" style="background-color:whitesmoke;padding-left:2px">
                <%-- EXPERIMENTAL --%>
                <a4j:commandLink value="Главная" onclick="document.location='ManagerGoodsPage.jsf'"/>
                <%--<a4j:commandLink value="Главная" ajaxSingle="true"--%>
                <%--oncomplete="document.location='ManagerGoodsPage.jsf'">--%>
                <%--<a4j:actionparam name="path_Id" assignTo="#{SessionBean.goodsFilter.goodTypeId}" value="-1"/>--%>
                <%--<a4j:actionparam name="searchOE" assignTo="#{SessionBean.goodsFilter.oe}" value=""/>--%>
                <%--</a4j:commandLink>--%>

                <a4j:repeat value="#{SessionBean.path}" var="p">
                    <h:outputText value=" / "/>
                    <a4j:commandLink value="#{p.name}" ajaxSingle="true"
                                     oncomplete="document.location='ManagerGoodsSearchPage.jsf'">
                        <a4j:actionparam name="path_Id" assignTo="#{SessionBean.goodsFilter.goodTypeId}"
                                         value="#{p.id}"/>
                        <a4j:actionparam name="searchOE" assignTo="#{SessionBean.goodsFilter.oe}" value=""/>
                    </a4j:commandLink>
                </a4j:repeat>
            </h:panelGroup>
        </h:panelGroup>

        <%-- Паджинатор --%>
        <h:panelGroup id="pager">

            <h:outputText value="<b>Страница:</b> #{ClientGoodsSearchBean.page} из #{ClientGoodsSearchBean.pagesCount}"
                          escape="false"/>

            <a4j:commandLink action="#{ClientGoodsSearchBean.toBegin}" value="[<<" reRender="tbManagerSearch,pager"
                             styleClass="pager_arr"/>
            <a4j:commandLink action="#{ClientGoodsSearchBean.longPrevPage}" value="<<" reRender="tbManagerSearch,pager"
                             styleClass="pager_arr"/>
            <a4j:commandLink action="#{ClientGoodsSearchBean.prevPage}" value="<" reRender="tbManagerSearch,pager"
                             styleClass="pager_arr"/>

            <h:panelGroup id="pager_nums">
                <a4j:repeat value="#{ClientGoodsSearchBean.pageIndexes}" var="pageIndex">
                    <a4j:commandLink id="test" action="#{ClientGoodsSearchBean.applyPage}" value="#{pageIndex.num}"
                                     styleClass="pager_page" reRender="tbManagerSearch,pager">
                        <a4j:actionparam name="page_id" assignTo="#{ClientGoodsSearchBean.page}"
                                         value="#{pageIndex.num}"/>
                    </a4j:commandLink>
                </a4j:repeat>
            </h:panelGroup>

            <a4j:commandLink action="#{ClientGoodsSearchBean.nextPage}" value=">" reRender="tbManagerSearch,pager"
                             styleClass="pager_arr"/>
            <a4j:commandLink action="#{ClientGoodsSearchBean.longNextPage}" value=">>" reRender="tbManagerSearch,pager"
                             styleClass="pager_arr"/>
            <a4j:commandLink action="#{ClientGoodsSearchBean.toEnd}" value=">>]" reRender="tbManagerSearch,pager"
                             styleClass="pager_arr"/>
        </h:panelGroup>
        <%-- --------- --%>
        <%-- -------------- Список найденных товаров -------------- --%>
        <rich:dataTable value="#{ClientGoodsSearchBean.goods}" var="good" id="tbManagerSearch" rowKeyVar="row"
                columnClasses="c0,c1,c2,c3,c4,c5,c6">
            <f:facet name="header">
                <h:outputText value="Список товаров"/>
            </f:facet>
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="Наименование" styleClass="b"/>
                </f:facet>
                <h:outputText value="#{good.name}"/>
            </rich:column>
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="Код"/>
                </f:facet>
                <h:outputText value="#{good.code}" styleClass="b"/>
            </rich:column>
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="o.e."/>
                </f:facet>
                <h:outputLink value="#oe" onclick="showOesModalPanel('#{good.code}')">
                    <h:outputText value="#{good.oeShort}" id="lOeShort"/>
                </h:outputLink>
            </rich:column>
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="Применимость"/>
                </f:facet>
                <h:outputText value="#{good.prShort}" id="lPrShort"/>
                <rich:toolTip for="lPrShort">
                    <h:outputText value="#{good.pr}"/>
                </rich:toolTip>
            </rich:column>
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="На складе"/>
                </f:facet>
                <h:outputText value="#{good.count} шт."/>
            </rich:column>
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="Резерв"/>
                </f:facet>
                <h:outputText value="#{good.reservedCount} шт."/>
            </rich:column>
            <rich:column>
                <f:facet name="header">
                    <h:panelGroup>
                        <h:outputText value="Цена<span class=\"star\">*</span>" escape="false"/>
                        <br>
                        <center>
                            <table cellpadding="0" cellspacing="0" border="0">
                                <tr>
                                    <a4j:repeat value="#{ClientGoodsSearchBean.clientGroups}" var="clientGroup">
                                        <td style="width:100px;text-align:center;">
                                            <h:outputText value="#{clientGroup.name}" style="font-size:80%;"/>
                                        </td>
                                    </a4j:repeat>
                                </tr>
                            </table>
                        </center>
                    </h:panelGroup>
                </f:facet>
                <center>
                    <table cellpadding="0" cellspacing="0" border="0">
                        <tr>
                            <a4j:repeat value="#{ClientGoodsSearchBean.clientGroups}" var="clientGroup">
                                <td style="width:100px;text-align:center;">
                                    <h:outputText value="#{good.pricesMap[clientGroup.id]}" converter="MoneyConverter"/>
                                </td>
                            </a4j:repeat>
                        </tr>
                    </table>
                </center>
            </rich:column>
            <rich:column>
                <h:panelGroup rendered="#{good.count > 0 && good.pricesMap[ClientGoodsSearchBean.client.clientgroups[good.brandId]] > 0}">
                    <h:inputText value="#{ClientGoodsSearchBean.count}"
                                 id="add_good_count"
                                 style="width:35px"/>
                    <h:outputText value="шт." style="margin-left:2px"/>
                    <h:outputLink value="#" onclick="addGoodInOrder(#{good.id}, #{rich:element('add_good_count')}.value);">
                        <h:outputText value="добавить"/>
                    </h:outputLink>
                </h:panelGroup>
                <h:panelGroup rendered="#{good.count == 0}">
                    <h:outputText value="товар отсутствует на складе" style="color:dimgray;font-size:80%"/>
                </h:panelGroup>
            </rich:column>
        </rich:dataTable>

    </h:form>

    <script type="text/javascript">
        function hideModalNotifications()
        {
            setTimeout(function() {
                Richfaces.hideModalPanel('modalNotifications');
            }, 1000);
        }
    </script>

    <rich:modalPanel id="modalNotifications" autosized="true" width="300" onshow="hideModalNotifications()">
        <a4j:outputPanel ajaxRendered="true">
            <h:form prependId="false">
                <rich:messages/>
            </h:form>
        </a4j:outputPanel>
    </rich:modalPanel>

    <jsp:include page="oes.jsp"/>
</f:view>
</body>
</html>